package com.mediacloud.app.newsmodule.view.share

import android.content.Context
import android.graphics.Bitmap
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.mediacloud.app.assembly.util.QRCodeUtil
import com.mediacloud.app.assembly.util.buildUrl
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.reslib.enums.XKey
import com.tencent.mmkv.MMKV
import java.net.URL

/**
 * 普通文稿的海报分享
 */
class PosterSharePop1(context: Context) : AbsPosterSharePop(context) {

    val posterRootView: View
    val allView: View
    val logoIv: ImageView
    val titleTv: TextView
    val summaryTv: TextView
    val qrCodeIv: ImageView
    val container: FrameLayout

    init {
        val lp = RelativeLayout.LayoutParams(-1, -2)
        lp.addRule(RelativeLayout.CENTER_IN_PARENT)
//        lp.marginStart = context.resources.getDimensionPixelSize(R.dimen.dimen48)
//        lp.marginEnd = context.resources.getDimensionPixelSize(R.dimen.dimen48)
        posterRootView = LayoutInflater.from(context).inflate(R.layout.share_poster1, null, false)
        logoIv = posterRootView.findViewById(R.id.logoIv)
        titleTv = posterRootView.findViewById(R.id.titleTv)
        summaryTv = posterRootView.findViewById(R.id.summaryTv)
        qrCodeIv = posterRootView.findViewById(R.id.qrCodeIv)
        container = posterRootView.findViewById(R.id.container)
        allView = posterRootView.findViewById(R.id.allView)
        container.setOnClickListener {
            dismiss()
        }
        posterContainer.addView(posterRootView, lp)
        if (MMKV.defaultMMKV().getBoolean(XKey.DARK_MODE, false)) {
            var dialogDarkMask =
                    layoutInflater.inflate(com.mediacloud.app.reslib.R.layout.mask_darkmode, null)
            if (MMKV.defaultMMKV().getBoolean(XKey.DARK_MODE, false)) {
                val lp = WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                                or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT
                )
                addContentView(dialogDarkMask, lp)
            }
            window?.navigationBarColor = getContext().getColor(R.color.navigation_dark_mode)
        }
    }

    override fun posterView(): View {
        return allView
    }

    override fun updateData(article: ArticleItem, bitmap: Bitmap?) {
        if (!TextUtils.isEmpty(article.shareTitle)) {
            titleTv.text = article.shareTitle
        } else {
            titleTv.text = article.real_title
        }
        if (article.isEPaper) {
            logoIv.setImageResource(R.drawable.see_jb_bg)
            logoIv.visibility = View.VISIBLE
        } else if (article.isGlobal24) {
            logoIv.setImageResource(R.drawable.bg_world24)
            logoIv.visibility = View.VISIBLE
            titleTv.text = "APP 环球24h"
        } else {
            if (TextUtils.isEmpty(article.logo)) {
                logoIv.visibility = View.GONE
                summaryTv.visibility = View.VISIBLE
                if ("pic" == article.share_poster?.type) {
                    logoIv.visibility = View.VISIBLE
                    summaryTv.visibility = View.GONE
                    if (bitmap != null) {
                        logoIv.setImageBitmap(bitmap)
                        logoIv.visibility = View.VISIBLE
                    } else {
                        loadX(context, buildUrl(3, article.share_poster?.value), 1, object : RequestListener<Drawable> {
                            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                                return true
                            }

                            override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                                logoIv.setImageDrawable(resource)
                                logoIv.visibility = View.VISIBLE
                                return true
                            }
                        })
                    }
                } else {
                    summaryTv.text = article.share_poster?.value
                }

            } else {
                summaryTv.visibility = View.GONE
                if (bitmap != null) {
                    logoIv.setImageBitmap(bitmap)
                    logoIv.visibility = View.VISIBLE
                } else {
                    loadX(context, buildUrl(3, article.logo), 1, object : RequestListener<Drawable> {
                        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                            return true
                        }

                        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                            logoIv.setImageDrawable(resource)
                            logoIv.visibility = View.VISIBLE
                            return true
                        }
                    })
                }
            }
        }
        if (!TextUtils.isEmpty(article.url)) {
            var urlStr = article.url
            try {
                val url = URL(urlStr)
                val files = url.query
                if (urlStr.contains("audioAlbum")) {
                    val split = urlStr.split("#")
                    urlStr = "${split[0]}?share=true&type=${article.type}&articleid=${article.id}#${split[1]}"
                }else{
                    urlStr += if (TextUtils.isEmpty(files)) {
                        "?share=true&type=${article.type}&articleid=${article.id}"

                    } else {
                        "&share=true&type=${article.type}&articleid=${article.id}"
                    }
                }

            } catch (e: Exception) {
                e.printStackTrace()
            }
            val qrCode = QRCodeUtil.createQRImage(urlStr, qrCodeIv.width, qrCodeIv.height, null)
            qrCodeIv.setImageBitmap(qrCode)
        }

        posterContainer.visibility = View.VISIBLE
    }


}